package com.ldg.manager.service.impl;

import com.ld.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.utils.Query;

import com.ldg.manager.dao.ItemCatDao;
import com.ldg.manager.entity.ItemCatEntity;
import com.ldg.manager.service.ItemCatService;

import javax.annotation.Resource;


@Service("itemCatService")
public class ItemCatServiceImpl extends ServiceImpl<ItemCatDao, ItemCatEntity> implements ItemCatService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ItemCatEntity> page = this.page(
                new Query<ItemCatEntity>().getPage(params),
                new QueryWrapper<ItemCatEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询所有分类，并以分类名为key，模板id为值存入redis
     * @return
     */
    @Override
    public List<ItemCatEntity> findAll() {
        //查询所有分类
        List<ItemCatEntity> itemCatEntities = baseMapper.selectList(null);
        //遍历并按需求存入redis
        for (ItemCatEntity itemCatEntity : itemCatEntities) {
            String name = itemCatEntity.getName();//分类名
            String typeid = itemCatEntity.getTypeId();//模板id
            redisTemplate.boundHashOps("itemCats").put(name,typeid);
        }
        return itemCatEntities;
    }

}
